//
//  QYXWeekRankingViewController.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXWeekRankingViewController.h"
#import "QYXTopRankingModel.h"
#import "QYXTopRankingTableViewCell.h"//重用cell
#import "QYXRankingInformationViewController.h"//详情页控制器
#import "AppDelegate.h"

@interface QYXWeekRankingViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 周排行表视图 */
@property (nonatomic, strong) UITableView *weekRankingTableView;

/** 周排行数据源数组 */
@property (nonatomic, strong) NSMutableArray *weekRankingDataSource;


@end

@implementation QYXWeekRankingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatWeekRankingView];
    [self getWeekRankingData];
    
}


- (void)creatWeekRankingView

{

 
//创建表视图:
    
    self.weekRankingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49 - 20) style:UITableViewStylePlain];
    self.weekRankingTableView.delegate = self;
    self.weekRankingTableView.dataSource = self;
    self.weekRankingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_weekRankingTableView];
//注册:
    
    [self.weekRankingTableView registerClass:[QYXTopRankingTableViewCell class] forCellReuseIdentifier:@"weekRankingCell"];
    

}



- (void)getWeekRankingData

{
    NSString *urlString = @"http://baobab.wandoujia.com/api/v3/ranklist?_s=68a5f23fa9ed299fe1acd4ef141b0d1d&f=iphone&net=wifi&strategy=weekly&u=aafd829df8ff2d571a778ed3cca27eb68b16bd7d&v=2.2.0&vc=721";
    
//自动缓存策略:
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    
    [QYXNetTool getNetWithUrl:urlString urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
         //开始解析数据:
                self.weekRankingDataSource = [NSMutableArray array];
        
        NSMutableDictionary *qyxDic = (NSMutableDictionary *)result;
        
        NSArray *array = [NSArray arrayWithArray:[qyxDic objectForKey:@"itemList"]];
        
                for (NSDictionary *dic in array) {
                    
                    NSMutableDictionary *tempDic = [dic objectForKey:@"data"];
        
                    QYXTopRankingModel *topRankingModel = [[QYXTopRankingModel alloc] initWithDic:tempDic];
                   
                        [self.weekRankingDataSource addObject:topRankingModel];
        
                }
        
                [self.weekRankingTableView reloadData];
      
    
    } failure:^(NSError *error) {
         NSLog(@"请求失败 : %@", error);
    }];
    
 
    
}

#pragma mark -- 签协议必须实现协议方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.weekRankingDataSource.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    QYXTopRankingModel *model = [self.weekRankingDataSource objectAtIndex:indexPath.row];
    
    QYXTopRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"weekRankingCell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.topRankingModel = model;
    
    cell.rankingNumber = indexPath.row + 1;

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return kScreenHeight / 3;

}


#pragma mark -- 点击跳转:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

     CATransition *transition = [CATransition animation];
     transition.duration = 0.5f;
     transition.delegate = self;
     transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
     transition.type = kCATransitionFade;
     transition.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    QYXRankingInformationViewController *rankingInforVC = [[QYXRankingInformationViewController alloc] init];
    rankingInforVC.RankingSelectNum = indexPath.row;
    rankingInforVC.rankingInforDataSource = self.weekRankingDataSource;
    AppDelegate *tempAppDe = [UIApplication sharedApplication].delegate;
    tempAppDe.tabBar.backView.hidden = YES;
    [self.navigationController pushViewController:rankingInforVC animated:NO];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
